from django.db import models
from django.db import connection
from django.db import transaction

from timesheet.models import Time

from manager.models import ProjectManagerComments
        
from manager_form import FormData
        

class HandleManagerForm:
    __confirm_form = FormData()
    __nonconfirm_form = FormData()
        
    def __init__(self, form):
        self.__confirm_form.filling_form(form, 'time_confirm_number', 'time_id_confirm_list', 
                        'time_id_confirm_', 'time_confirm_', 'comments_', 0)
        self.__nonconfirm_form.filling_form(form, 'time_nonconfirm_number', 'time_id_nonconfirm_list', 
                        'time_id_nonconfirm_', 'time_nonconfirm_', 'comments_', 1)
        
    def __validating_and_updating_time_list(self, form):
        cursor = connection.cursor()
        require = Time.take_time.take_time_list_query(form.time_id_row)
        cursor.execute(require)
        for row in cursor.fetchall():
            if row[1]!=form.time_list[unicode(row[0])] or row[2]!=0 or row[3]!=1:
                form.delete_time_line(row[0])
        form.generate_time_id_row()
       
    @transaction.commit_on_success       
    def handle(self, manager_id):
        if self.__confirm_form.form_state==u'valid':
            self.__validating_and_updating_time_list(self.__confirm_form)
            if self.__confirm_form.form_state==u'valid':
                Time.take_time.confirm(self.__confirm_form.time_id_row)
        if self.__nonconfirm_form.form_state==u'valid':
            self.__validating_and_updating_time_list(self.__nonconfirm_form)
            if self.__nonconfirm_form.form_state==u'valid':
                Time.take_time.turn_down_confirm(self.__nonconfirm_form.time_id_row)
                ProjectManagerComments.objects.insert_comments(manager_id, 
                                self.__nonconfirm_form.time_number, 
                                self.__nonconfirm_form.time_id_list, 
                                self.__nonconfirm_form.comments_list)
        if self.__confirm_form.form_state==u'invalid' or self.__nonconfirm_form.form_state==u'invalid':
            return unicode(self.__confirm_form.error_report) + unicode(self.__nonconfirm_form.error_report)
        else:
            return self.print_form()
        
    def print_form(self):
        form = []
        form.append(u'confirm_form: ')
        form.append(u'time_number: ' + unicode(self.__confirm_form.time_number))
        form.append(u'time_id_row: ' + unicode(self.__confirm_form.time_id_row))
        form.append(u'time_id_list: ' + unicode(self.__confirm_form.time_id_list))
        form.append(u'time_list: ' + unicode(self.__confirm_form.time_list))
        form.append(u'comments_list: ' + unicode(self.__confirm_form.comments_list))
        form.append(u'form_state: ' + unicode(self.__confirm_form.form_state))
        form.append(u'error_report: ' + unicode(self.__confirm_form.error_report))
        form.append(u'nonconfirm_form: ')
        form.append(u'time_number: ' + unicode(self.__nonconfirm_form.time_number))
        form.append(u'time_id_row: ' + unicode(self.__nonconfirm_form.time_id_row))
        form.append(u'time_id_list: ' + unicode(self.__nonconfirm_form.time_id_list))
        form.append(u'time_list: ' + unicode(self.__nonconfirm_form.time_list))
        form.append(u'comments_list: ' + unicode(self.__nonconfirm_form.comments_list))
        form.append(u'form_state: ' + unicode(self.__nonconfirm_form.form_state))
        form.append(u'error_report: ' + unicode(self.__nonconfirm_form.error_report))
        return form